//index.js
//获取应用实例
import { config } from '../../utils/config.js';
import { CouponDetailModel } from '../../model/coupon-detail.js';
import { showLoading,  hideLoading } from '../../components/mask.js';
import { formatDate } from '../../utils/util';
const couponDetailModel = new CouponDetailModel();

Page({
  data: {
	valideMsg: '',
	couponTitle: '优惠 :',
	couponTipTitle: '使用须知 :',
	couponInstanceCode: '',
	encryCouponInstanceCode: '',
	couponDetail: {},
	couponSmallPath: "",
	couponBigPath: "",
	couponComments: [], // 使用须知
	couponStoresComments: [], // 适用门店区域
	couponPrice: [],
	isShowBigImage: false,
	couponStoreTitle: '适用门店区域',
	checkStoreTitle: '查看适用门店详情'
  },
  
  onLoad: function(option) {
	  showLoading();
	  this.setData({
		  couponInstanceCode: option.couponInstanceCode
	  });
	  this._loadCouponDetail();
  },
  
  // 查看门店列表
  checkCouponStore: function(e) {
	  let storeGroupId = e.currentTarget.dataset.storeid;
	  if(storeGroupId) {
		  jd.navigateTo({
		  		url: '/pages/coupon-store/index?storeGroupId=' + storeGroupId
		  });
	  }
  },
  
  // 处理图片放大
  handleZoomImage: function () {
	  this._loadRefreshCouponCode();
  },
  
  // 从子组件接受的参数
  handleCloseImage: function (e) {
	  if( e.detail && e.detail.isShowBigImage) {
		  this.setData({
			  isShowBigImage: e.detail.isShowBigImage
		  });
	  } else {
		  this.setData({
		  	  isShowBigImage: false
		  });
	  }
  },
  
  // 初始化数据
  _loadCouponDetail: function() {
	  let userOpenId = jd.getStorageSync('openId') || app.globalData.openId;
	  const couponDetail = couponDetailModel.getCouponInstanceDetail(
	  					userOpenId, this.data.couponInstanceCode);
	  // const couponDetail = couponDetailModel.getCouponInstanceDetail(
			// 			'PRNPDoouG1PVHZsMFIeNCCFBTKo=', 'CI548059600035');
	  couponDetail.then((res) => {
	  		  this.setData({
	  			  couponDetail: res.data.couponInstanceExp,
	  			  couponSmallPath: res.data.couponSmallPath,
	  			  couponBigPath: res.data.couponBigPath,
				  encryCouponInstanceCode: res.data.couponInstanceExp.couponInstanceCode,
	  			  valideMsg: this._handleValideDate(res.data.couponInstanceExp.startDate, res.data.couponInstanceExp.expireDate),
				  couponPrice: this._handleCouponPrice(res.data.couponInstanceExp.remark),
	  			  couponComments: this._handleCouponComments(res.data.couponInstanceExp.jdComments),
				  couponStoresComments: this._handleCouponComments(res.data.couponInstanceExp.jdStoretext)
	  		  });
			  hideLoading();
	  });
  },
  
  // 刷新优惠券实例编码
  _loadRefreshCouponCode: function() {
	  const refreshCouponDetail = couponDetailModel.getRefreshCouponCode(this.data.couponDetail.couponInstanceCode);
	  refreshCouponDetail.then((res) => {
		  this.setData({
			  isShowBigImage: true,
			  encryCouponInstanceCode: res.data.couponInstanceCode,
			  // couponSmallPath: res.data.couponSmallPath,
			  couponBigPath: res.data.couponBigPath
		  });
	  });
  },
  
  // 处理价格对比
  _handleCouponPrice: function(remark) {
	  let msg = [];
	  if(remark) {
		  msg = remark.split("，");
	  }
	  return msg;
  },
  
  // 处理使用须知
  _handleCouponComments: function(comments) {
	  let couponComments = [];
	  if(comments) {
		  couponComments = comments.split("@@");
	  }
	  return couponComments;
  },
  
  // 处理有效日期
  _handleValideDate: function(startDate, expireDate) {
	  let start = "";
	  let expire = "";
	  if(startDate) {
		  start = formatDate(startDate);
	  }
	  
	  if(expireDate) {
		  expire = formatDate(expireDate);
	  }
	  return "有效期" + start + "至" + expire;
  }
});